package com.atguigu.tingshu.dispatch.job;

import com.atguigu.tingshu.search.client.SearchFeignClient;
import com.atguigu.tingshu.user.client.UserFeignClient;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class DispatchHandler {

    @Autowired
    private SearchFeignClient searchFeignClient;


    /**
     * 入门案例：任务
     */
    @XxlJob("myJob")
    public void task1() {
        //获取任务参数
        String jobParam = XxlJobHelper.getJobParam();
        log.info("task1....{}", jobParam);

    }


    /**
     * 定时任务：远程调用搜索服务更新专辑热门排行榜
     *
     * @return
     */
    @XxlJob("updateAlbumRanking")
    public ReturnT updateAlbumRanking() {
        try {
            log.info("正在执行更新排行榜-远程调用搜索服务更新专辑热门排行榜");
            searchFeignClient.updateLatelyAlbumRanking();
            return ReturnT.SUCCESS;
        } catch (Exception e) {
            log.error("[任务服务]时任务更新排行榜异常：{}", e);
            return ReturnT.FAIL;
        }
    }


    @Autowired
    private UserFeignClient userFeignClient;


    /**
     * 定时任务：远程调用用户服务更新会员状态
     *
     * @return
     */
    @XxlJob("updateVipExpireStatus")
    public ReturnT updateVipExpireStatus() {
        try {
            log.info("正在执行更新会员过期-远程调用用户服务更新会员状态");
            userFeignClient.updateVipExpireStatus();
            return ReturnT.SUCCESS;
        } catch (Exception e) {
            log.error("[任务服务]定时任务更新VIP异常：{}", e);
            return ReturnT.FAIL;
        }
    }

    /**
     * 短信提醒扣款通知，30w个任务
     */
    @XxlJob("shardjob")
    public void  shardjob(){
        int shardTotal = XxlJobHelper.getShardTotal();
        log.info("总分片数：{}", shardTotal);
        int shardIndex = XxlJobHelper.getShardIndex();
        log.info("当前执行器索引：{}", shardIndex);
    }
}
